//
//  VisitorView.swift
//  Weibo
//
//  Created by 赵志文 on 15/11/22.
//  Copyright © 2015年 zzw. All rights reserved.
//

import UIKit
//protocol VisitorViewDelegate:NSObjectProtocol{
//    func visitorViewWillRegester()
//    func visitorViewWillLogin()
//    
//}

class VisitorView: UIView {
    
    //定义代理
//    weak var delegate :VisitorViewDelegate?
    
//    @objc private func clickRegestrBtn(){
//        delegate?.visitorViewWillRegester()
//    }
//    
//    @objc private func clickLoginBtn(){
//        delegate?.visitorViewWillLogin()
//    }
    
    func setUpInfo(imageName :String?, messsage:String){
        messageLabel.text = messsage
        
        //判断是否传递图片
        if let imgName = imageName{
            iconView.image = UIImage(named: imgName)
            homeIcon.hidden = true
            sendSubviewToBack(maskIconView)
        }else{
            startAnimation()
        }
    }
    private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 10
        animation.repeatCount = MAXFLOAT
        //设置动画不被删除。当iconView被销毁的时候，动画就会停止
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    /// 纯代码开发调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    //sb开发会调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    //MARK:界面布局
/// 设置界面
    private func setUpUI(){
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //设置布局
        /*
        view1.attr1 = view2.attr2 * multiplier + constant
        */
        //1.icon
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        //小房子
        homeIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //文本
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //提示：如果要设置一个固定的数值，需要设置NSLayoutAttribute.NotAnAttribute，参照对象为nil
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //注册按钮
        registButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        //遮罩视图
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[regButton]", options: [], metrics: nil, views: ["v":maskIconView,"regButton" :loginButton]))
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1);
    
    }
    
    //图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //小房子
    private lazy var homeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //消息文字
    private lazy var messageLabel :UILabel = {
        let label = UILabel()
        label.text = "关注一些人，点击这里有惊喜关注一些人"
        //一般不要使用纯黑色的（让人不舒服）
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
        
    }()
    //注册按钮
    lazy var registButton :UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
    }()
    //登录按钮
    lazy var loginButton : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
        
    }()
}
